package features.advance.leetcode.interview.offer.easy;

import com.alibaba.fastjson.parser.JSONToken;

/**
 * @author LIN
 * @date 2021-04-30
 */
public class Offer03 {
    public int findRepeatNumber(int[] nums) {
        int n = nums.length;
        if(n<=1){
            return nums[0];
        }

        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            arr[nums[i]]++;
            int o = arr[nums[i]];
            if(o > 1){
                return nums[i];
            }
        }
        return nums[0];
    }

    public static void main(String[] args) {
        int[] nums = {2,1,3,4,2,0,6,4,3};
        int repeatNumber = new Offer03() {
            @Override
            public int findRepeatNumber(int[] nums) {
                for (int i = 0; i < nums.length; i++) {
                    while(nums[i] != i) {
                        if (nums[nums[i]] == nums[i]){
                            return nums[i];
                        }
                        int tmp = nums[i];
                        nums[i] = nums[tmp];
                        nums[tmp] = tmp;
                    }
                }
                return -1;
            }
        }.findRepeatNumber(nums);
        System.out.println(repeatNumber);
    }
}
